let api = require('./api')
// 跳转方法
function jumpUrl(type, url) {
  if (type == 0) {
    wx.navigateTo({
      url: url,
    })
  } else if (type == 1) {
    wx.navigateTo({
      url: '/pages/webview/webview?src=' + url,
    })
  } else if (type == 2) {
    wx.navigateToMiniProgram({
      appId: 'wxb82568e4dfcf56ac',
      path: url,
      success(res) {
        // 打开成功
      }
    })
  }
};
// 扫描二维码方法
function ScanQRcode(result) {
  let that = this;
  if (result.data.code == 200) {
    var productId = result.data.data.scanButtonType;
    var productImg1 = result.data.data.productImg1;
    var point = result.data.data.point;
    console.log(' -- - - -- - -- >', result)
    wx.navigateTo({
      url: '/pages/interests/interests?point='+point+'&img=' + productImg1 + '&id=' + productId,
    })
  } else if (result.data.code == 4000) {
    wx.navigateTo({
      url: '/pages/isnotcorrect/isnotcorrect?code=' + result.data.code,
    })
  } else if (result.data.code == 4001) {
    wx.navigateTo({
      url: '/pages/isnotcorrect/isnotcorrect?code=' + result.data.code,
    })
  } else if (result.data.code == 4002) {
    wx.navigateTo({
      url: '/pages/isnotcorrect/isnotcorrect?code=' + result.data.code,
    })
  } else {
    that.data.alert.show({
      content: result.data.message,
    });
  }
}
// 扫描二维码 测试码与正式码 判断方法
function Qrcodelength(qrcode) {
  if (qrcode.length > 43) {
    qrcode = qrcode.substring(0, 43)
  } else if (qrcode.length > 38 && qrcode.length < 43) {
    qrcode = qrcode.substring(0, 38)
  }
}
// 格式化日期
const formatDate = function (d) {
  if (!d) {
    return '';
  }
  if (typeof d == 'Date') {
    d = d.toString();
  }
  const date = new Date(d.replace(/-/g, '/'));
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();

  return [year, month, day].map(formatNumber).join('-');
};

// 格式化日期时间
const formatDateTime = function (d) {
  if (!d) {
    return '';
  }
  if (typeof d == 'Date') {
    d = d.toString();
  }
  const date = new Date(d.replace(/-/g, '/'));
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  const hour = date.getHours();
  const minute = date.getMinutes();
  const second = date.getSeconds();

  return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':');
};

// 格式化时分秒，个位前补0
const formatNumber = function (n) {
  n = n.toString();
  return n[1] ? n : '0' + n;
};

// 日期相差天数
const dayDifference = function (d1, d2) {
  if (!d1 || !d2) {
    return 0;
  }
  const data1 = new Date(d1.replace(/-/g, '/'));
  const data2 = new Date(d2.replace(/-/g, '/'));
  var endTime = data1.getTime() / 1000 - data2.getTime() / 1000;
  return parseInt(endTime / 60 / 60 / 24);
};

// 获取当前年价
const getYear = function () {
  const date = new Date();
  return date.getFullYear();
};
// 新加检测方法
const tagOperation = function (number, OPERATION) {
  let y = 0 + "" + OPERATION
  let type = 'OPERATION_' + (OPERATION < 10 ? y : OPERATION)
  console.log(wx.getStorageSync('logincode'), '- - -- - -- 监测代码')
  if (wx.getStorageSync('logincode') == 502) {
    return
  }
  api.interface.tagOperation({
    count: number,
    operationType: type,
  }).then(res => { })

}
module.exports = {
  jumpUrl: jumpUrl,
  formatDate: formatDate,
  formatDateTime: formatDateTime,
  dayDifference: dayDifference,
  getYear: getYear,
  ScanQRcode: ScanQRcode,
  Qrcodelength: Qrcodelength,
  tagOperation: tagOperation,
};